const express = require("express");
const app = express();
const mongoose = require('mongoose');
const bodyParser = require("body-parser");
const passport = require("passport");

const usersRouter=require("./routes/api/users.js");
const postsRouter=require("./routes/api/posts.js");
const commentRouter=require("./routes/api/comments.js");
const greatRouter=require("./routes/api/greats.js");
const readRouter=require("./routes/api/reads.js");
const collRouter=require("./routes/api/collections.js");
const followRouter=require("./routes/api/follows.js");

//连接mongodb数据库
mongoose.Promise=global.Promise;
mongoose.connect('mongodb://localhost/mySystem', {useNewUrlParser: true,useUnifiedTopology:true})
.then(()=>console.log('connection successful!'))
.catch((err)=>console.error(err));

//加载bodyParser中间件
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());

//初始化passport
app.use(passport.initialize());
require("./config/passport")(passport);

//设置路由
app.use("/api/users",usersRouter);
app.use("/api/posts",postsRouter);
app.use("/api/comments",commentRouter);
app.use("/api/greats",greatRouter);
app.use("/api/reads",readRouter);
app.use("/api/colls",collRouter);
app.use("/api/follows",followRouter);

const port = process.env.PORT || 5000;

app.listen(port,()=>{
    console.log(`Server running on port ${port}`);
})

